package com.prothink.threadadmindemo.config;

import com.prothink.threadadmindemo.interceptor.CorsInterceptor;
import com.prothink.threadadmindemo.interceptor.JWTTokenInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author mayanping
 * @title: InterceptorConfig
 * @projectName ThreadAdminDemo
 * @description: TODO
 * @date 2024/9/509:04
 */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
    private static List<String> whiteList = new ArrayList<>();

    static {
        whiteList.add("/user/register");
        whiteList.add("/user/login");
        whiteList.add("/user/resetPwd");
        whiteList.add("/user/validUserAccount");
        whiteList.add("/user/validIdCard");
    }

    @Bean
    CorsInterceptor corsInterceptor(){
        return new CorsInterceptor();
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        /**
         * 拦截全部路径，这个跨域需要放在最上面
         */
        registry.addInterceptor(corsInterceptor()).addPathPatterns("/**");

        //登陆拦截器
        registry.addInterceptor(new JWTTokenInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(whiteList);

        WebMvcConfigurer.super.addInterceptors(registry);
    }
}
